package com.googlecode.tidej.client.project;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.googlecode.tidej.client.Dialog;
import com.googlecode.tidej.client.TidejController;
import com.googlecode.tidej.client.PersistentState;
import com.googlecode.tidej.client.project.ProjectMetaData.TargetData;
import com.googlecode.tidej.client.project.ProjectMetaData.TargetType;
import com.googlecode.tidej.client.util.HttpDataSource;


public class ConnectionDialog extends Dialog {
  public enum Mode {
    UPLOAD, DOWNLOAD,
  }

  TextBox urlTextBox = new TextBox();
  TextBox userTextBox = new TextBox();
  TextBox passwordTextBox = new PasswordTextBox();
  PersistentState projects;
  FlowPanel mainPanel = new FlowPanel();
  TidejController controller;
  TargetType type;
  

  public ConnectionDialog(TidejController controller, ProjectMetaData.TargetType type) {
    super(type == TargetType.DEPLOYMENT ? "Deploy Project" : "Development Server Connection");
    this.type = type;
    this.controller = controller;
    FlexTable table = new FlexTable();
    
    table.setText(0, 0, "URL");
    table.setText(1, 0, "User");
    table.setText(2, 0, "Password");

    table.setWidget(0, 1, urlTextBox);
    table.setWidget(1, 1, userTextBox);
    table.setWidget(2, 1, passwordTextBox);

    Button downloadButton = new Button("Download");
    downloadButton.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        confirm(Mode.DOWNLOAD);
      }
    });
    addButton(downloadButton);

    Button uploadButton = new Button("Upload");
    uploadButton.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        confirm(Mode.UPLOAD);
      }
    });
    addButton(uploadButton);
    
    getContentPanel().add(table);
  }
  
  public void run() {
    ProjectMetaData projectEntry = controller.getProjectManager().getEntry();
    TargetData data = projectEntry.getTargetData(type);
    urlTextBox.setText(data.getUrl());
    userTextBox.setText(data.getUser());
    passwordTextBox.setText(data.getPassword());
    show();
    center();
  }
  
  void confirm(Mode mode) {
    ProjectManager projectManager = controller.getProjectManager();
    ProjectMetaData projectEntry = projectManager.getEntry();
    
    // TODO(haustein) Check for differences before saving...
    
    TargetData data = projectEntry.getTargetData(type);
    data.setUrl(urlTextBox.getText());
    data.setUser(userTextBox.getText());
    data.setPassword(passwordTextBox.getText());

    HttpDataSource dataSource = new HttpDataSource(data.getUrl());
    dataSource.authenticate(data.getUser(), data.getPassword());
    
    projectEntry.save();
    
    if (mode == Mode.UPLOAD) {
      projectManager.deploy(dataSource);
    } else {
      String name = projectManager.getName();
      controller.closeProject(false);
      controller.openProject(name, null);
    }
    
    hide();
  }
}
